<template>
    <div class="app-container">
        <el-form :inline="true" ref="queryRef" :rules="rules" label-position="right" :model="params" label-width="200px">
            <div class="form-inline">
                <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="区域：" class="form-item" prop="region">
                            <el-form-item prop="province" style="width:120px;margin-right:10px;">
                                <el-select v-model="params.province" placeholder="请选择省" @change="chooseProvince" clearable
                                    value-key="id">
                                    <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>

                            <el-form-item prop="city" style="width:120px;margin-right:10px;">
                                <el-select v-model="params.city" placeholder="请选择市" @change="chooseCity" clearable
                                    value-key="id">
                                    <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>

                            <el-form-item prop="county" style="width:120px;">
                                <el-select v-model="params.county" placeholder="请选择县" @change="chooseCounty" clearable
                                    value-key="id">
                                    <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="结构类型：" class="form-item" prop="st">
                            <el-col :span="11">
                                <el-form-item prop="structureType">
                                    <el-select v-model="params.structureType" placeholder="请选择" class="form-select"
                                        @change="structureTypeChange" clearable>
                                        <el-option v-for="item in structureTypeOptions" :key="item.value"
                                            :label="item.label" :value="item.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="1" class="text-center">
                                <span class="text-gray-500">-</span>
                            </el-col>
                            <el-col :span="11">
                                <el-form-item prop="structureType1">
                                    <el-select v-model="params.structureType1" placeholder="请选择" class="form-select"
                                        clearable>
                                        <el-option v-for="item in structureType1Options" :key="item.value"
                                            :label="item.label" :value="item.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="设防情况：" class="form-item" prop="fortification">
                            <el-select v-model="params.fortification" placeholder="请选择" class="form-select" clearable>
                                <el-option v-for="item in fortificationOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="年代：" class="form-item" prop="years">
                            <el-select v-model="params.years" placeholder="请选择" class="form-select" clearable>
                                <el-option v-for="item in yearsOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row justify="end">
                    <el-form-item class="bottom-button">
                        <el-button type="primary" @click="onSearch">查询</el-button>
                        <el-button type="primary" @click="onRest">重置</el-button>
                    </el-form-item>
                </el-row>
            </div>
        </el-form>


        <div class="table-wrapper">
            <el-table class="listTable" ref="multipleTableRef" border :data="tableData" style="width: 100%"
                @selection-change="handleSelectionChange" :cell-style="{ border: '1px solid #e5ebf7' }"
                :header-cell-style="{ border: '1px solid #e5ebf7', borderLeft: 'none' }">


                <el-table-column prop="number" header-align="center" align="center" label="编号">
                    <template #default="scope">
                        <el-button link type="text" size="small" @click.prevent="viewProject(scope.row)">
                            {{ scope.row.number }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="fortification" header-align="center" align="center" label="设防情况" />
                <el-table-column prop="region" header-align="center" align="center" label="小震" />
                <el-table-column prop="userName" header-align="center" align="center" label="中震" />
                <el-table-column prop="st" header-align="center" align="center" label="大震" />
                <el-table-column prop="use" header-align="center" align="center" label="综合情况" />
                <el-table-column label="操作" fixed="right">
                    <template #default="scope">
                        <el-button type="text" size="small" @click="onDelete(scope.row)">删除</el-button>
                        <!-- <el-button type="text" size="small" @click="onModify(scope.row)">修改</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
            <div class="paging">
                <el-pagination background v-model:current-page="queryPagination.pageNo" :layout="layout"
                    :page-size="queryPagination.pageSize" :total="queryPagination.total"
                    @current-change="handleCurrentChange" @size-change="handleSizeChange" />
            </div>

        </div>


    </div>
</template>
 
<script setup>
import { reactive, toRefs } from "vue";
import { getInfo } from '@/api/login'
import { getUserId } from "@/utils/auth";
import { updateBuildings, deleteBuildingsById, getBuildingsById, queryBuildings } from "@/api/importData/buildingDamageData.js";
import { getProvince, getChildArea } from "@/api/system/division";
const { proxy } = getCurrentInstance()
const router = useRouter();
const data = reactive({

    // 表单参数
    params: {
        structureType: "", //结构类型
        structureType1: "",
        fortification: "", //设防情况
        years: "",//年代
        region: "",//区域
    },
    rules: {
        region: [
            { required: true, message: '请选择', trigger: 'change' }
        ],
        st: [
            { required: true, message: '请选择', trigger: 'change' }
        ],
        fortification: [
            { required: true, message: '请选择', trigger: 'change' }
        ],
        years: [
            { required: true, message: '请选择', trigger: 'change' }
        ],
    },
    fortificationOptions: [
        //候选值为：未设防、6度、7度、8度、9度、自设防、其他
        {
            value: '未设防',
            label: '未设防'
        },
        {
            value: '6度',
            label: '6度'
        }, {
            value: '7度',
            label: '7度'
        }, {
            value: '8度',
            label: '8度'
        }, {
            value: '9度',
            label: '9度'
        }, {
            value: '自设防',
            label: '自设防'
        }, {
            value: '其他',
            label: '其他'
        }
    ],
    yearsOptions: [
        {
            value: '1978年以前',
            label: '1978年以前'
        }, {
            value: '1978-1989年',
            label: '1978-1989年'
        }, {
            value: '1989-2001年',
            label: '1989-2001年'
        }, {
            value: '2001年以后',
            label: '2001年以后'
        }, {
            value: '全部',
            label: '全部'
        }
    ],
    structureTypeOptions: [
        {
            value: '1',
            label: '砌体结构',
            children: [
                {
                    value: 'zhjg',
                    label: '砖混结构',
                }, {
                    value: 'smjg',
                    label: '石木结构',
                },
                {
                    value: 'zmjg',
                    label: '砖木结构',
                }]
        }, {
            value: '2',
            label: '钢筋混凝土结构',
            children: [
                {
                    value: 'kjjg',
                    label: '框架结构',
                }, {
                    value: 'jlqjg',
                    label: '框架-剪力墙结构',

                }, {
                    value: 'jlqjg',
                    label: '剪力墙结构'
                }]
        }, {
            value: '3',
            label: '工业厂房',
            children: [{
                value: 'zhjgcf',
                label: '砖混结构厂房'
            }, {
                value: 'gjhntjgcf',
                label: '钢筋混凝土结构厂房'
            }, {
                value: 'gjgcf',
                label: '钢结构厂房'
            }]
        }
    ],
    structureType1Options: [],
    //表格
    tableData: [],
    //分页
    layout: " prev, pager, next, jumper, sizes, total",
    queryPagination: {
        pageNo: 1,
        pageSize: 10,
        total: 100,
    },
})



const provinceData = ref([]); //省
const cityData = ref([]); //市
const areaData = ref([]); //区、县

/** 获取省列表 */
function getProvinceData() {
    getProvince().then((response) => {
        provinceData.value = response.data;
    });
}

function chooseProvince(value) {
    params.value.city = null;
    params.value.county = null;
    params.value.province = value.name;
    cityData.value = [];
    areaData.value = [];
    getChildArea(value.id).then(response => {
        cityData.value = response.data;
    });
    /*provinceData.value.map((item) => {
      //遍历数据
      if(item.id == value.id){
              if(item.name == "北京市"||item.name == "天津市"||item.name == "上海市"||item.name == "重庆市"){
              cityData.value.push(JSON.parse(JSON.stringify(item)));
              }else{
                  getChildArea(value.id).then(response => {
                  cityData.value = response.data;
                  });
              }
              return;
          }
    });*/
}

function chooseCity(value) {
    params.value.city = value.name;
    params.value.county = null;
    areaData.value = [];
    getChildArea(value.id).then((response) => {
        areaData.value = response.data;
    });
}

function chooseCounty(value) {
    params.value.county = value.name;
}
const {
    params,
    fortificationOptions,
    yearsOptions,
    structureTypeOptions,
    structureType1Options,
    tableData,
    queryPagination,
    layout,
    rules
} = toRefs(data);

// watch(params.value, (val) => {
//     if (val.createStartTime && val.createEndTime) {
//         const startDate = val.createStartTime
//         const endDate = val.createEndTime
//         const startTime = new Date(startDate).getTime()
//         const createEndTime = new Date(endDate).getTime()
//         if (startTime > createEndTime) {
//             proxy.$message.error('结束日期必须大于开始日期，请重新选择！')
//             val.createEndTime = ''
//         }
//     }
// })

// const startDisableDate = (time) => {
//     let endDateVal = params.value.createEndTime
//     if (endDateVal) {
//         return time.getTime() > new Date(endDateVal).getTime()
//     }
// }

// const endDisableDate = (time) => {
//     let beginDateVal = params.value.createStartTime
//     if (beginDateVal) {
//         return time.getTime() < new Date(beginDateVal).getTime()
//     }
// }

const structureTypeChange = (val) => {
    const structureTypes = structureTypeOptions.value.find(item => item.value === val)
    if (structureTypes) {
        structureType1Options.value = structureTypes.children
    }
}


// 获取用户
// function getUserInfo() {
//     getInfo(getUserId()).then(response => {
//         params.value.userName = response.user.userName
//         params.value.maintainerUnit = response.user.orgName
//         params.value.createTime = parseTime(new Date())
//     });
// };


//表格
const multipleTableRef = ref();

const handleSelectionChange = (val) => {
    //   multipleSelection.value = val;
    //   curTableRow.value = val[0];
};
//分页
const handleSizeChange = (val) => {
    // 改变每页显示的条数 
    queryPagination.value.pageSize = val
    // 注意：在改变每页显示的条数时，要将页码显示到第一页
    queryPagination.value.pageNo = 1
    onSearch()
};
const handleCurrentChange = (val) => {
    onSearch()
};


// 点击编号跳转至此数据的详细信息界面
const viewProject = (row) => {
    router.push({ path: '/disasterDataManagement/maintainData/buildVulnerabilityInfo/', query: { number: row.number } })
}


//建筑物区域特征信息维护表格查询
const onSearch = () => {
    const query = {
        ...params.value,
        page: queryPagination.value.pageNo,
        rows: queryPagination.value.pageSize
    }

    queryBuildings(query).then((res) => {
        if (res.code === 200) {
            console.log(res)
            queryPagination.value.total = res.data.total
            tableData.value = [];
            const rows = res.data.rows;
            //循环遍历之前加上非空判断
            if (rows.length >= 0) {
                rows.forEach((item) => {
                    tableData.value.push({
                        cbhdId: item.cbhdId,
                        number: item.number,
                        region: item.region,
                        st: item.st,
                        years: item.years,
                        fortification: item.fortification,
                        userName: item.userName,
                        createTime: item.createTime,
                        intensity: item.intensity,
                        damage: item.damage,
                        use: item.use,
                        cs: item.cs,
                    });
                });
            }
        }
    }).catch(err => {
        console.log(err);
    }
    );
};

//重置
const onRest = () => {
    proxy.resetForm("queryRef")
    onSearch();
};

//导入
const onImport = () => {
    router.push({ path: '/disasterDataManagement/importData/buildingDamageData' })
}

//修改
// const onModify = (row) => {
//     router.push({ path: '', query: { id: 'modify', type: row.type } })
// }

//删除
const onDelete = (row) => {
    console.log(row)
    const query = { number: row.number }
    deleteBuildingsById(query).then((res) => {
        if (res.code === 200) {
            console.log(res)
            proxy.$modal.msgSuccess("删除成功");
            onSearch();
        }
    }).catch(err => {
        console.log(err);
    }
    )
}

onMounted(() => {
    onSearch()
    getProvinceData();
    //getUserInfo();
});
</script>
 
<style lang="scss">
.table-wrapper {
    position: relative;

    h4 {
        font-size: 16px;
        font-weight: bold;
        color: #107ce5;
        text-align: center;
    }
}

.paging {
    padding: 20px 0;
}

.form-inline {
    .el-form-item {
        margin-right: 0;
    }
}
</style>


